#pdf("nomogram.pdf")
Dat$Stratification<-pbccox$linear.predictors

library(survival)
library(survminer)

#write.csv(inputdata,"inputdata.csv")  

Dat<-as.data.frame(Dat)
res.cut<- surv_cutpoint(Dat, #数据集
                         time ="time" , #生存状态
                         event = "status", #生存时间
                         variables = colnames(Dat)[18],
                         minprop = 0.3#需要计算的数据列名
)


summary(res.cut) #查看数据最佳截断点及统计量
res.cut$cutpoint[1]
dat_haf<-Dat[Dat$Stratification<3.444747,]

res.cut1<- surv_cutpoint(dat_haf, #数据集
                        time ="time" , #生存状态
                        event = "status", #生存时间
                        variables = colnames(Dat)[18],
                        minprop = 0.3#需要计算的数据列名
)

res.cut1$cutpoint[1]


Dat$Risk_Stratification <- ifelse(Dat$Stratification >= 1.812739, "High_risk",
                      ifelse(Dat$Stratification < -0.8286012, "Low_risk", "moderate_risk"))





fit <- survfit(Surv(dat[,1], dat[,2]) ~Risk_Stratification, data = dat)

ggsurvplot(fit,
           conf.int=F, #置信区间
           fun="event",
           pval=TRUE,
           palette = c("#F40002","#01847F","#FBD26A"),
           pval.method = T,
           risk.table =T,
           ncensor.plot = F,
           surv.median.line="hv",
           ylab="Cumulative Death Rate (%)",
           legend.labs=c('High_risk','Low_risk',"moderate_risk"))+
        labs(x = "Years")



plot(x, 
     lplabel="Risk Stratification",
     xfrac = 0.2, # 左侧标签距离坐标轴的距离
     #varname.label = TRUE, 
     tcl = -0.2, # 刻度长短和方向 
     lmgp = 0.1, # 坐标轴标签距离坐标轴远近
     points.label ='Points', 
     total.points.label = 'Total Points',
     cap.labels = FALSE,
     cex.var = 1, # 左侧标签字体大小
     cex.axis = 1, # 坐标轴字体大小
     col.grid = gray(c(0.8, 0.95))) # 竖线颜色
rect(0.26,0.20,0.5,0.26,col = "#01847F") # 添加彩色条带
rect(0.5,0.20,0.7,0.26,col = "#FBD26A")
rect(0.7,0.20,0.96,0.26,col = "#F40002")
text(0.4,0.18,"Low")
text(0.6,0.18,"Medium")
text(0.83,0.18,"High")
#dev.off()


#在底部再增加3个彩色条带，高度错开，显得有层次感
rect(0.37,0.14,0.5,0.144,col = "#01847F")
rect(0.5,0.144,0.7,0.148,col = "#FBD26A")
rect(0.7,0.148,0.835,0.152,col = "#F40002")
#如果你还要继续添加文字说明也可以，我这里就不加了
#dev.off()
加了

# 添加箭头
arrows(0.205,0.86,0.205,0.96,col = "steelblue",lwd = 4,length = 0.1)
arrows(0.4,0.76,0.4,0.96,col = "steelblue",lwd = 4,length = 0.1)
arrows(0.68,0.655,0.68,0.96,col = "steelblue",lwd = 4,length = 0.1)
arrows(0.28,0.55,0.28,0.96,col = "steelblue",lwd = 4,length = 0.1)
arrows(0.47,0.45,0.47,0.96,col = "steelblue",lwd = 4,length = 0.1)

# 总分箭头，加起来可能不对，单纯演示下
arrows(0.84,0.40,0.84,0.35,col = "#F40002",lwd = 4,length = 0.1)
#dev.off()
